<?php
/**
 * User Update Screen
 * 
 * @author Xavier VILAIN
 * @package 
 * @license #hive#
 * 
 */

use Fox\Html\DropList;
use Fox\Html\Input;
use Fox\Authorization;
use Fox\Behaviors;
use Fox\Html\Button;
use Fox\Generator;
use Fox\Html\Screen;

$xview = Xview::instance();
$idusr = $xview->val('idusr');

// Check Authorization
if(!Authorization\Manager::check(Hive\Customizing::AUTH_ADMIN_USERS_W)) {
	$xview->accessDenied(Msg(Hive::BUNDLE,Hive::ERR_ADMIN_ACCES_DENIED),null,true);
	exit;
}

try {
	$user = new User($idusr);
} catch(Fox\Object\Exception\NotExist $e) {
	
}
$data = $user->getData();
if($user->exists()) {
	$mode = 'isave';
	$title = Msg(Hive::BUNDLE,Hive::USER_UPDATE_TITLE,$user->name());
	$data['proid'] = $user->application()->profileId();
	$data['proid'] = empty($data['proid']) ? $user->proid : $data['proid'];
} else {
	$mode = 'create';
	$title = Msg(Hive::BUNDLE,Hive::USER_CREATE_TITLE);
}
$screen = new Screen('userUpdate','update',null,true);
$screen->action(Generator::url('admin','users','update',array('idusr' => $idusr)));
$screen->mainTitle($title);
// Block Header
$blHeader = new Fox\Html\Block('bInfo',Msg(Hive::BUNDLE,Hive::GENERAL_INFORMATION));
$blHeader->add(new Input('email',true))->size(Input::SIZE_LARGE);
$blHeader->add(new Input('fname',true));
$blHeader->add(new Input('lname',true));
$blHeader->add(new DropList('proid',true,false,true))
 			->options($xview->application()->profiles(),'proid',Generator::call('label'));// Profile
$screen->add($blHeader);
// Personal Settings
$blLocale = new Fox\Html\Block('bLocale',Msg(Hive::BUNDLE,Hive::LOCALE_SETTINGS));
$blLocale->add(new DropList(array('langu','language'),false,false,true))
			->options($xview->application()->languages(),'langu',Generator::call('description'));
$blLocale->add(new DropList('dpara',false,true,false));
$blLocale->add(new DropList('npara',false,true,false));
$screen->add($blLocale);

$screen->initialize($data);

if($mode == 'isave') {
	
	$screen->toolbar()->add(new Button('back'))
			->behaviors(Behaviors::action(
					Generator::url('admin','users','list'),
					false,
					null,
					null,
					false,
					'GET',
					null,
					null,
					'contentCol'
				))
		->label(Msg(Hive::BUNDLE,Hive::USER_BACK_TO_LIST));
	$screen->toolbar()->add(new Button('delete'))
			->behaviors(Behaviors::action('admin','users','delete'))
			->color(Button::COLOR_RED);
			
	$blHeader->item('email')->input(false);
}
if($user->isNotActive()) {
	$screen->add(new Button('activation'))
			->color(Button::COLOR_WARNING)
			->label(Msg(Hive::BUNDLE,Hive::RESEND_ACTIVATION_EMAIL));
}

if($screen->submitted()) {
	$fields = $screen->get();
	if(!$screen->errors()) {
		//
		try {
			$user->setFields($fields);
			// Update Profile
			$user->application()->setFields(array('proid' => $fields['proid']));
			if($_POST['pushed'] == 'activation') {
				$activation = $user->activation();
				if(empty($activation)) {
					$activation = new \Fox\User\Activation();
					$user->addToActivation($activation);
				} else {
					$user->activation()->regenerate();
				}
				$user->update();
				$id = $user->getId();
				// Sent the activation account link to the user
				$link = str_replace('#language#',$fields['language'],_CLOUD_ACTIVATION_URL_);
				$message = $user->activation()->sendActivationKey(
					Msg(Hive::BUNDLE,Hive::ACTIVATION_EMAIL_TITLE),
					array(Hive::BUNDLE,Hive::ACTIVATION_EMAIL_BODY),
					$link,
					array(Hive::BUNDLE,Hive::EMAIL_TEMPLATE),
					_APPLICATION_DEFAULT_EMAIL_FROM_
				);
				// Attach the current application to the user
				Fox\Message::Success(Msg(Hive::BUNDLE,Hive::ACTIVATION_EMAIL_SENT_TO,$user->email));
			} else {
				$user->update();
			}
		}
		catch (Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		}
	}
	echo $xview->messages(true,true);
	exit;
}
if($user->isNotActive()) {
	Fox\Message::Warning(Msg(Hive::BUNDLE,Hive::USER_IS_NOT_ACTIVE_YET));
} 
echo $screen->display();